﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows;
using Wpf.Ui.Controls;
using ShareCode.Dto;

namespace ClientAppraise.Controls
{
    public class ZxDrawerRoutedEventArgs : RoutedEventArgs
    {
        public ZxDrawerRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }
        public bool Result
        {
            get; set;
        }
    }


    public class ZxDrawer : ContentControl
    {
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
        nameof(Title),
        typeof(object),
        typeof(ZxDrawer)
    );

        public static readonly RoutedEvent OpenedEvent = EventManager.RegisterRoutedEvent(
            nameof(Opened),
            RoutingStrategy.Bubble,
            typeof(TypedEventHandler<ZxDrawer, RoutedEventArgs>),
            typeof(ZxDrawer)
        );

        public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent(
            nameof(Closed),
            RoutingStrategy.Bubble,
            typeof(TypedEventHandler<ZxDrawer, ZxDrawerRoutedEventArgs>),
            typeof(ZxDrawer)
        );


        public object Title
        {
            get => GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        /// <summary>
        /// Occurs after the dialog is opened.
        /// </summary>
        public event TypedEventHandler<ZxDrawer, RoutedEventArgs> Opened
        {
            add => AddHandler(OpenedEvent, value);
            remove => RemoveHandler(OpenedEvent, value);
        }

        /// <summary>
        /// Occurs after the dialog is closed.
        /// </summary>
        public event TypedEventHandler<ZxDrawer, ZxDrawerRoutedEventArgs> Closed
        {
            add => AddHandler(ClosedEvent, value);
            remove => RemoveHandler(ClosedEvent, value);
        }

        ResultDto<object>? CurrentReturn;
        TaskCompletionSource<ResultDto<object>>? Tcs;
        readonly ContentPresenter contentPresenter;
        public ZxDrawer(ContentPresenter Content)
        {
            contentPresenter = Content;

            Loaded += static (sender, _) =>
            {
                var self = (ZxDrawer)sender;
                self.OnLoaded();
            };
        }

        protected virtual void OnLoaded()
        {
            var bs = (BeginStoryboard)this.Template.FindName("BSDrawerBoardOut", this);

            bs.Storyboard.Completed += (sender, e) =>
            {
                if (CurrentReturn == null) {
                    CurrentReturn = new ResultDto<object>() { Code = 400 };
                }

                Tcs?.TrySetResult(CurrentReturn);
            };

            Focus();
        }

        public async Task<ResultDto<object>> ShowAsync(UserControl userControl)
        {
            Title = userControl.Tag;
            this.Content = userControl;

            // var bs = (Border)this.Template.FindName("ZxDrawerBorder", this);
            // bs.Width = userControl.Width;

            Tcs = new TaskCompletionSource<ResultDto<object>>();
            var result = new ResultDto<object>();
            try
            {
                contentPresenter.Content = this;
                OnOpened();
                result = await Tcs.Task;
                return result;
            }
            finally
            {
                contentPresenter.Content = null;

                OnClosed(result.Success);
            }
        }
        public virtual void Hide(ResultDto<object> result)
        {
            CurrentReturn = result;
            var bs = (System.Windows.Controls.Button)this.Template.FindName("ZxDrawerColseButton", this);
            RoutedEventArgs eventArgs = new RoutedEventArgs(System.Windows.Controls.Button.ClickEvent);
            bs.RaiseEvent(eventArgs);
        }
        public virtual void Hide(bool b)
        {
            CurrentReturn = new ResultDto<object> { Code = b ? 200 : 400 };
            var bs = (System.Windows.Controls.Button)this.Template.FindName("ZxDrawerColseButton", this);
            RoutedEventArgs eventArgs = new RoutedEventArgs(System.Windows.Controls.Button.ClickEvent);
            bs.RaiseEvent(eventArgs);
        }

        protected virtual void OnOpened()
        {
            RoutedEventArgs closedEventArgs = new RoutedEventArgs(OpenedEvent, this);
            RaiseEvent(closedEventArgs);
        }
        protected virtual void OnClosed(bool result)
        {
            ZxDrawerRoutedEventArgs closedEventArgs = new ZxDrawerRoutedEventArgs(ClosedEvent, this) { Result = result };
            RaiseEvent(closedEventArgs);
        }


    }

}
